package com.example.jpatest.controller.restful;

import com.example.jpatest.entity.Competition;
import com.example.jpatest.entity.Enter;
import com.example.jpatest.entity.Team;
import com.example.jpatest.entity.Win;
import com.example.jpatest.exception.enterException.NoEnterException;
import com.example.jpatest.service.CompetitionService;
import com.example.jpatest.service.EnterService;
import com.example.jpatest.service.TeamService;
import com.example.jpatest.service.WinService;
import com.example.jpatest.unit.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class WinController {

    @Autowired
    WinService winService;

    @Autowired
    CompetitionService competitionService;

    @Autowired
    TeamService teamService;

    @Autowired
    EnterService enterService;

    /**
     * 通过报名id获奖
     */
    @PostMapping("/win/enterId={enterId}")
    public Result win(@PathVariable Long enterId,
                      @RequestParam(name = "winLevel") Win.WinLevel winLevel,
                      @RequestParam(name = "prize") int prize){
        Enter enter = enterService.getById(enterId);
        if(enter==null){
            throw new NoEnterException();
        }
        winService.win(enter,prize,winLevel);
        return Result.succMsg();
    }

    /**
     * 通过id取消报名获奖
     */
    @DeleteMapping("/win/enterId={enterId}")
    public Result win(@PathVariable Long enterId){
        Enter enter = enterService.getById(enterId);
        if(enter==null){
            throw new NoEnterException();
        }
        winService.cancelWin(enter);
        return Result.succMsg();
    }

    /**
     * 通过报名id更新获奖信息
     */
    @PutMapping("/win/enterId={enterId}")
    public Result updateWin(@PathVariable Long enterId,
                            @RequestBody Win win){
        Enter enter = enterService.getById(enterId);
        if(enter==null){
            throw new NoEnterException();
        }
        enter.setWin(winService.getUpdatedWin(win,enter));
        enterService.saveEnter(enter);
        return Result.succMsg();
    }

    /**
     * 获得所有获奖信息
     */
    @GetMapping("/wins")
    public Result getWins(){
        return Result.succ(winService.getAllWin());
    }
}
